package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaomi.pojo.*;
import com.xiaomi.service.AddressService;
import com.xiaomi.service.CartService;
import com.xiaomi.service.GoodsTypeService;
import com.xiaomi.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName OrderServlet
 * @Author xiao宋
 * @Date 20:27  2022/9/15
 * @Version 1.0
 * @Description TODO
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    CartService cartService;
    @Autowired
    OrderService orderservice;
    @Autowired
    AddressService addressService;
    @Autowired
    GoodsTypeService goodsTypeService;

    /**
     * 获取订单列表
     *
     * @return
     */
    @RequestMapping("/getOrderList")
    public ModelAndView getOrderList(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Order> orderList = orderservice.getOrderList(user.getId());
        System.out.println("getOrderList处的id" + user.getId());
        ModelAndView mv = new ModelAndView();
//        req.setAttribute("orderList", orders);
        mv.addObject("orderList", orderList);
        mv.setViewName("forward:/orderList.jsp");

//        return "forward:/orderList.jsp";
        return mv;
    }

    /**
     * 支付成功
     *
     * @return
     */
    @RequestMapping("/paySuccess")
    public ModelAndView paySuccess(String result, HttpSession session) {

        JSONObject jsonresult = JSON.parseObject("result");

        Object result1 = jsonresult.get("result");
        JSONObject jsonObject = JSON.parseObject(result1.toString());

        String return_code = jsonObject.getString("return_code");
        String orderId = jsonObject.getString("out_trade_no");

        if ("SUCCESS".equalsIgnoreCase(return_code)) {
            //如果支付成功。更新订单状态
            orderservice.updateOrderStatus(orderId, "2");
        }

        return getOrderList(session);
    }

    /**
     * 添加订单
     *
     * @return
     */
    @RequestMapping("/addOrder")
    public ModelAndView addOrder(HttpSession session, Integer aid) {
        User user = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        Order order = orderservice.addOrder(aid, user.getId());
        mv.addObject("order", order);
        return mv;
    }

    /**
     * 获取订单详情
     *
     * @param oid
     * @return
     */
    @RequestMapping("/getOrderDetail")
    public ModelAndView getOrderDetail(String oid) {

        OrderDetail orderDetail = orderservice.getOrderDetail(oid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("od", orderDetail);
        mv.setViewName("forward:/orderDetail.jsp");
        return mv;
    }

    /**
     * 给用户订单获取购物车信息
     *
     * @param req
     * @return
     */
    @RequestMapping("/getOrderView")
    public String getOrderView(HttpServletRequest req, HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Cart> carts = cartService.getCarts(user.getId());
//        获取 地址列表
        List<Address> addlist = addressService.getAddrList(user.getId());
        //把数据添加到请求域
        req.setAttribute("carts", carts);
        req.setAttribute("addList", addlist);
//          请求转发到前端
        return "forward:/order.jsp";
    }

    /**
     * 确认收货
     */
    @RequestMapping("/changeStatus")
    public ModelAndView changeStatus(String oid, HttpSession session) {
        goodsTypeService.changeStatus(oid);
        return getOrderList(session);
    }
    //********************************************************************************************
    /**
     * 发货
     */
    @RequestMapping("/adminSendOrder")
    public ModelAndView sendOrder(String oid,HttpSession session) {
        //订单 ID
        //通过订单 ID 查找到订单并把 status从 2 改成 3
        orderservice.sendOrder(oid);

        return getAllOrder(session);
        //  return "forward:/admin/showAllOrder.jsp";
    }
    /**
     * 获取订单信息。
     * @return
     */
    @RequestMapping("/adminGetAllOrder")
    public ModelAndView getAllOrder(HttpSession session) {
        System.out.println("获取所有订单");
        User user = (User) session.getAttribute("admin");
        System.out.println(user);
        List<Order> orderList = orderservice.getUserAllOrderById(user.getId());
        ModelAndView mv = new ModelAndView();
        System.out.println(orderList);
        mv.addObject("orderList", orderList);
        mv.setViewName("forward:/admin/showAllOrder.jsp");
        return mv;
    }
    /**
     * 搜索用户
     * @return
     */
    @RequestMapping("/adminSearchOrder")
    public String searchOrder(String username, String status, HttpServletRequest req) {
        List<Order> orderList = orderservice.searchOrder(username, status);
        System.out.println(orderList);
        req.setAttribute("orderList", orderList);
        return "forward:/admin/showAllOrder.jsp";
    }
}
